#include <iostream>
#include<algorithm>
#include <string.h>
#include<stack>
#include<vector>
#include<map>
#include<string>
using namespace std;
int main()
{
	int n,t=0;
	while (cin>>n)
	{
		t++;
		if (!n)break;
		map<string, int>Num_name;
		for (int i = 0; i < n; i++)
		{
			string name;
			cin >> name;
			Num_name[name] = i;
		}
		int m;
		cin >> m;
		vector< vector<double> >mpt(n, vector<double>(n, 0));
		for (int i = 0; i < m; i++)
		{
			string name1, name2; double val;
			cin >> name1 >> val >> name2;
			mpt[Num_name[name1]][Num_name[name2]] = val;
		}
		for (int k = 0; k < n; k++)
		{
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					mpt[i][j] = max(mpt[i][j], mpt[i][k] * mpt[k][j]);
				}
			}

		}
		int flag = 0;
		for (int i = 0; i < n; i++)
		{
			if (mpt[i][i] > 1)flag = 1;
		}
		if (flag)
			cout << "Case " << t << " Yes" << endl;
		else
			cout << "Case " << t << " No" << endl;
	}
	return 0;
}